﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using XamlInspector.Foundation;
using XamlInspector.Model.Resources;

namespace XamlInspector.Model
{
    public static class ResourceHelper
    {
        private static readonly List<XamlResource> _appResources;
        private static string[] _themeResources;

        static ResourceHelper()
        {
            _appResources = GetResources(Application.Current.Resources, ResourceScope.App);
        }

        public static async Task<IEnumerable<XamlResource>> GetResources(FrameworkElement element)
        {
            // Local and inherited resources
            var resources = GetResources(element.Resources);

            resources.AddRange(_appResources);

            // Theme resources
            resources.AddRange(await GetThemeResources());

            return resources;
        }

        private static List<XamlResource> GetResources(ResourceDictionary dictionary, ResourceScope scope = ResourceScope.Local)
        {
            var resources = new List<XamlResource>();

            foreach (var key in dictionary.Keys)
            {
                resources.Add(CreateResource(key.ToString(), dictionary[key], scope));
            }

            if (scope == ResourceScope.Local)
            {
                scope = ResourceScope.Inherited;
            }

            foreach (var mergedDict in dictionary.MergedDictionaries)
            {
                resources.AddRange(GetResources(mergedDict, scope));
            }

            return resources;
        }

        private async static Task<List<XamlResource>> GetThemeResources()
        {
            var appResources = Application.Current.Resources;
            var themeResources = new List<XamlResource>();

            if (_themeResources == null)
            {
                var fileContent = EmbeddedResourceProvider.GetFile("ThemeResources.csv");
                _themeResources = fileContent.Split(new[] { "\n", "\r", " " }, StringSplitOptions.RemoveEmptyEntries);
            }

            foreach (var key in _themeResources)
            {
                themeResources.Add(CreateResource(key, appResources[key], ResourceScope.Theme));
            }

            return themeResources;
        }

        private static XamlResource CreateResource(string key, object value, ResourceScope scope)
        {
            var brush = value as Brush;

            if (brush != null)
            {
                return new XamlBrushResource(key, brush, scope);
            }

            if (value is FontWeight)
            {
                return new XamlFontWeightResource(key, (FontWeight)value, scope);
            }

            var family = value as FontFamily;
            if (family != null)
            {
                return new XamlFontFamilyResource(key, family, scope);
            }

            return new XamlResource(key, value, scope);
        }
    }
}
